;; 补全插件配置

(require 'init-package)

(when (maybe-require-package 'corfu)
  (dolist (hook
           '(prog-mode-hook eshell-mode-hook lisp-interaction-mode-hook))
    (add-hook hook 'corfu-mode))

  (add-hook 'corfu-mode-hook
            (lambda ()
              (custom-set-variables
               '(corfu-auto t)
               '(corfu-auto-prefix 1)
               ;;'(corfu-auto-delay 0)
               '(corfu-min-width 30)
               '(corfu-cycle t)
               '(corfu-preselect-first t)
               '(corfu-quit-no-match 'separator)
               '(corfu-quit-at-boundary t)
               '(corfu-preview-current nil)
               '(corfu-echo-documentation nil))
              ;; (custom-set-faces         ;设置面板颜色
              ;;  '(corfu-default ((t (:foreground "black" :background "#ccefe8")))))

              (define-key corfu-map (kbd "<escape>") 'corfu-quit)
              (define-key corfu-map [tab] 'corfu-next)
              (define-key corfu-map [backtab] 'corfu-previous)
              (define-key corfu-map (kbd "M-d") 'corfu-info-documentation)
              (define-key corfu-map (kbd "M-l") 'corfu-info-location)
              )))

(with-eval-after-load 'corfu
  (when (maybe-require-package 'corfu-terminal)
    (unless (display-graphic-p)
      (progn
        (require 'corfu-terminal)
        (corfu-terminal-mode +1))))

  (when (maybe-require-package 'cape)
    (global-set-key (kbd "C-c p p") 'completion-at-point)
    (global-set-key (kbd "C-c p f") 'cape-file)
    (global-set-key (kbd "C-c p l") 'cape-line)
    (global-set-key (kbd "C-c p w") 'cape-dict)

    (add-to-list 'completion-at-point-functions #'cape-dabbrev)
    (add-to-list 'completion-at-point-functions #'cape-file)
    ;;(add-to-list 'completion-at-point-functions #'cape-keyword)
    ;;(add-to-list 'completion-at-point-functions #'cape-tex)
    ;;(add-to-list 'completion-at-point-functions #'cape-sgml)
    ;;(add-to-list 'completion-at-point-functions #'cape-rfc1345)
    ;;(add-to-list 'completion-at-point-functions #'cape-abbrev)
    ;;(add-to-list 'completion-at-point-functions #'cape-ispell)
    ;;(add-to-list 'completion-at-point-functions #'cape-dict)
    ;;(add-to-list 'completion-at-point-functions #'cape-symbol)
    ;;(add-to-list 'completion-at-point-functions #'cape-line)
    ;;(add-to-list 'completion-at-point-functions #'cape-history)
    )

  ;; 使用flex和substring代替
  ;; (when (maybe-require-package 'orderless)
  ;;   (setq completion-styles '(orderless basic) ; '(orderless flex)
  ;;         completion-category-defaults nil
  ;;         completion-category-overrides '((file (styles basic partial-completion)))
  ;;         orderless-matching-styles '(orderless-literal orderless-regexp orderless-flex)))
  )

(provide 'init-corfu)
